package com.leet.code.lcof;

import com.algorithm.model.TreeNode;

import java.util.LinkedList;
import java.util.List;

public class Code32_3 {

    public static void main(String[] args) {

    }


    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> rsList = new LinkedList<>();

        if (root == null) {
            return rsList;
        }

        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.addLast(root);
        boolean b = false;
        while (queue.size() != 0) {
            int s = queue.size();
            LinkedList<Integer> list = new LinkedList<>();
            for (int i = 0; i < s; i++) {
                TreeNode t;
                if (b) {
                    t = queue.removeFirst();
                    if (t.right != null) {
                        queue.addLast(t.right);
                    }
                    if (t.left != null) {
                        queue.addLast(t.left);
                    }
                } else {
                    t = queue.removeLast();
                    if (t.left != null) {
                        queue.addFirst(t.left);
                    }
                    if (t.right != null) {
                        queue.addFirst(t.right);
                    }
                }
                list.add(t.val);
            }
            rsList.add(list);
            b = !b;
        }

        return rsList;

    }


}
